home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 November: Tool Chest / Dev.CD Nov 96 TC / Dev.CD Nov 96 TC.toast / Sample Code / Snippets / QuickDraw / ScreenDump / DumpScreen.c next >
Encoding:
Text File  |  1994-12-13  |  2.7 KB  |  114 lines  |  [TEXT/KAHL]

  1. // This snippet shows how to dump an area of the screen.
  2. //
  3. //    Copyright:    © 1989-94 by Apple Computer, Inc..
  4.  
  5. #include <QDOffscreen.h>
  6. #include <PictUtil.h>
  7.  
  8.  
  9. // dump an area of the screen
  10. // return a PicHandle or nil
  11. PicHandle DumpScreenArea()
  12. {
  13.     CGrafPtr    wPort ;
  14.     CGrafPtr    savedPort ;
  15.     GDHandle    oldDevice ;
  16.     PicHandle    thePict = nil ;
  17.     
  18.     GWorldPtr    theNewWorld ;
  19.     GDHandle    gDevice;
  20.     
  21.     Rect        gDeviceRect;
  22.     Rect        intersectingRect;
  23.     Point        anchorPt ;
  24.     
  25.     OSErr        theErr ;
  26.     Rect         area ;
  27.     
  28.     RgnHandle    grayRgn = GetGrayRgn() ;
  29.     Rect        wideOpenRect = (**grayRgn).rgnBBox ;
  30.     
  31.     Rect RubberBandIt(Point    anchorPt) ;
  32.  
  33.     // save our world
  34.     GetGWorld( &savedPort, &oldDevice ) ;
  35.         
  36.     // get the window managers grafport
  37.     GetCWMgrPort( &wPort ) ;
  38.  
  39.     // and make the window managers grafport our current port
  40.     SetPort( (GrafPtr)wPort ) ;
  41.  
  42.     ClipRect( &wideOpenRect ) ;
  43.     
  44.     while (!Button())
  45.         GetMouse(&anchorPt);        // get the current mouse pos.
  46.         
  47.     area = RubberBandIt(anchorPt) ;
  48.  
  49.     // set up a deep - 32 bit - GWorld (you don't have to use GWorld stuff, though,
  50.     // see forrest tanaka's principia of offscreen tech note in the 
  51.     // imaging/graphics technotes for alternative methods).
  52.     // we do this to keep things simple
  53.     
  54.     if((theErr = NewGWorld( &theNewWorld, 32, &area, nil, nil, 0L )) != noErr)
  55.         return nil ; ;
  56.     
  57.     SetGWorld( (CGrafPtr)theNewWorld, nil ) ;
  58.  
  59.     // Get the handle to the first device in the global device list.
  60.     // loop through the device list, checking if the rect defined by area 
  61.     // intersects the device
  62.     
  63.     for( gDevice = GetDeviceList(); gDevice != nil; gDevice = GetNextDevice( gDevice )) {
  64.         
  65.         // Get the device's gdRect and convert it to local coordinates.
  66.         gDeviceRect = (**gDevice).gdRect;
  67.             
  68.         GlobalToLocal( &topLeft( gDeviceRect ) );
  69.         GlobalToLocal( &botRight( gDeviceRect ) );
  70.         
  71.         // Check if the app's window rect intersects the device's, and if it
  72.         // does, set the clip region's rect to the intersection and copy into
  73.         // our offscreen buffer
  74.         
  75.         if (SectRect( &area, &gDeviceRect, &intersectingRect ))
  76.         {
  77.             ClipRect( &intersectingRect );
  78.             CopyBits( (BitMap *)(*(**gDevice).gdPMap), 
  79.                       (BitMap *)(*(*theNewWorld).portPixMap), 
  80.                       &intersectingRect, 
  81.                       &intersectingRect, 
  82.                       srcCopy, 
  83.                       nil );
  84.         }
  85.     }
  86.     
  87.     SetGWorld( (CGrafPtr)theNewWorld, nil ) ;
  88.     
  89.     thePict = OpenPicture( &area );
  90.     if(thePict != nil ) {
  91.     
  92.         ClipRect( &area );
  93.         
  94.         CopyBits( (BitMap *)(*(*theNewWorld).portPixMap), 
  95.                   (BitMap *)(*(*theNewWorld).portPixMap), 
  96.                   &area, 
  97.                   &area, 
  98.                   srcCopy, 
  99.                   nil );
  100.                   
  101.         ClosePicture();
  102.  
  103.     }
  104.     
  105.     FlushEvents( mDownMask, 0 ) ;
  106.     
  107.     // restore the world
  108.     SetGWorld( savedPort, oldDevice ) ;
  109.     DisposeGWorld( theNewWorld );
  110.     
  111.     // return the pict
  112.     return thePict ;
  113. }
  114.